
import requests
import re
import json
import jsonpath
s = requests.session()
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
def get_response(url):
    response=s.get(url,headers=headers)
    if response.status_code==200:
        return response.text
    else:
        print("请求出错！")

def pinglun(nid,user_id):
    pinglun_url = "https://rate.tmall.com/list_detail_rate.htm?itemId={itemid}&spuId={spuId}&sellerId={sellerId}&order=3"
    reJson = s.get(pinglun_url.format(itemid=nid,spuId=user_id,sellerId=user_id))
    #f =  str(reJson.text).find("jsonp128",0,10)
    if(True):
        # 一号接口
        JSONP = str(reJson.text).replace("jsonp128","")
        Json =re.findall(r'.*?({.*}).*',JSONP,re.S)[0]
        j = json.loads(Json)
        rateList =  j['rateDetail']['rateList']
        page = 2
        while(len(rateList) > 0):
             # 评论项
            for i in rateList:
                content = i['rateContent'] # 评论的内容
                rateDate = i['rateDate'] # 评论时间
                auctionSku = str(i['auctionSku'])# 购买的物品类型
                print(content)
                print(rateDate)
                print(auctionSku)
            # 下一页链接
            pinglun_url = "https://rate.tmall.com/list_detail_rate.htm?itemId={itemid}&spuId={spuId}&sellerId={sellerId}&currentPage={page}&order=3"
            reJson = s.get(pinglun_url.format(itemid=nid,spuId=user_id,sellerId=user_id,page=page))
            JSONP = str(reJson.text).replace("jsonp128","")
            Json =re.findall(r'.*?({.*}).*',JSONP,re.S)[0]
            j = json.loads(Json)
            rateList =  j['rateDetail']['rateList']
            page += 1
        

       

    else:
        # 二号接口 有问题只能获取近一个月的评论
        taobao_url = 'https://rate.taobao.com/feedRateList.htm?auctionNumId={auctionNumId}&userNumId={userNumId}&currentPageNum=1&pageSize=1000000'
        reJson = s.get(taobao_url.format(auctionNumId=nid,userNumId=user_id))
        JSONP = str(reJson.text).replace("jsonp_tbcrate_reviews_list","")
        Json =re.findall(r'.*?({.*}).*',JSONP,re.S)[0]
        j = json.loads(Json)
        comments = j['comments']
        for i in comments:
            print(i['content'])


def get_info(html):
    nid = re.findall('"nid":"(.*?)"',html)# 商品id
    user_id = re.findall('"user_id":"(.*?)"',html)# 店铺id
    name_pat = re.findall('"raw_title":"(.*?)"',html)# 商品名称
    nick_pat = re.findall('"nick":"(.*?)"',html)# 店铺名称
    price_pat = re.findall('"view_price":"(.*?)"',html)# 商品价格
    sales_pat = re.findall('"view_sales":"(.*?)"',html)# 付款人数
    city_pat = re.findall('"item_loc":"(.*?)"',html)# 店铺所在地
   
    info_list=[]

    for i in range(len(name_pat)):
        info=[]
        info.append(nid[i])
        info.append(user_id[i])
        info.append(name_pat[i])
        info.append(price_pat[i])
        info.append(sales_pat[i])
        info.append(nick_pat[i])
        info.append(city_pat[i])
        pinglun(info[0],info[1])
        info_list.append(info)
        print(info_list) # 打印
    return info_list

def user_login(username,password):
    param = {
        'TPL_password':password,   
        'TPL_username':username,  
    }
    login_path = 'https://login.taobao.com/member/login.jhtml'
    s.post(login_path,params=param,headers=headers)


if __name__ == '__main__':
    user_login('15268335587','zhao261618')
    for page in range(0,10):
        url ='https://s.taobao.com/search?q={q}&bcoffset=0&ntoffset=0&s={s}'.format(q="衣服男",s=page*48)
        html=get_response(url)
        info=get_info(html)